package com.scau.yangsuiyu.estore.controller.myComponent;

import com.jfoenix.controls.JFXToggleButton;
import com.scau.yangsuiyu.estore.controller.seller.EditGoodsPane;
import com.scau.yangsuiyu.estore.controller.seller.manageShop.ShopGoodsManagePane;
import com.scau.yangsuiyu.estore.dababases.dao.GoodsDao;
import com.scau.yangsuiyu.estore.dababases.dao.ShopDao;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.GoodsPhoto;
import com.scau.yangsuiyu.estore.dababases.entity.Shop;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 商品的HBox
 * @date 2019-06-09 18:06
 * @email suiyu_yang@163.com
 */
public class ShopGoodsHBox extends HBox implements Initializable {
    private Goods goods;
    private Shop shop;
    private ShopGoodsManagePane shopGoodsManagePane;
    @FXML
    private Label idLabel;

    @FXML
    private Label categoryLabel;

    @FXML
    private Label goodsNameLabel;

    @FXML
    private ImageView photoImageView;

    @FXML
    private Label priceLabel;

    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label salesLabel;

    @FXML
    private Label inventoryLabel;

    @FXML
    private Label createTimeLabel;

    @FXML
    private Label lastUpdateTimeLabel;

    @FXML
    private JFXToggleButton statusButton;

    public ShopGoodsHBox(Goods goods, ShopGoodsManagePane shopGoodsManagePane) {
        this.goods = goods;
        this.shop = goods.getShop();
        this.shopGoodsManagePane = shopGoodsManagePane;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/ShopGoodsHBox.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        updateGoodsToUI();
    }

    /**
     * 更新商品信息至UI
     */
    public void updateGoodsToUI() {
        idLabel.setText(goods.getId() + "");
        categoryLabel.setText(goods.getCategory().getName());
        goodsNameLabel.setText(goods.getGoodsName());
        priceLabel.setText("¥" + goods.getPrice() + "");
        promotionalPriceLabel.setText("¥" + goods.getPromotionPrice() + "");
        salesLabel.setText(goods.getSales() + "");
        inventoryLabel.setText(goods.getInventory() + "");
        createTimeLabel.setText(goods.getCreateTime().toLocalDateTime().toString());
        updateUIGoodsStatus();

        if (goods.getLastUpdateTime() != null) {
            lastUpdateTimeLabel.setText(goods.getLastUpdateTime().toLocalDateTime().toString());
        }

        //加载首图
        List<GoodsPhoto> photoList = goods.getGoodsPhotoList();
        if (photoList.size() != 0) {
            GoodsPhoto goodsPhoto = photoList.get(0);
            ImageViewUtil.loadImage(photoImageView, goodsPhoto.getPhotoUrl());
        }

    }

    @FXML
    private void modifyClick() {
        EditGoodsPane editGoodsPane = new EditGoodsPane(this);
        editGoodsPane.showPane();
    }

    @FXML
    private void deleteClick() {
        shop.removeGoods(goods);

        new GoodsDao().delete(goods);
        new ShopDao().update(shop);
        shopGoodsManagePane.updateUI();
    }

    @FXML
    private void changeStatusClick() {
        if (goods.isDisable()) { //此时状态为"下架"
            //上架商品
            goods.setDisable(false);
        } else {
            //下架商品
            goods.setDisable(true);
        }
        new GoodsDao().update(goods);

        updateUIGoodsStatus();
        shopGoodsManagePane.updateUI();
    }

    private void updateUIGoodsStatus() {
        if (goods.isDisable()) { //此时状态为"下架"
            statusButton.setText("已下架");
            statusButton.setSelected(false);
        } else {
            statusButton.setText("已上架");
            statusButton.setSelected(true);
        }
    }

    public Goods getGoods() {
        goods = new GoodsDao().getGoodsById(goods.getId());
        return goods;
    }

}
